//
//  ActivityViewController.m
//  UI12豆瓣大项目
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//
#define WIDTH  [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height


#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "DetailViewController.h"
#import "AppTools.h"
#import "Activity.h"
#import "MJRefresh.h"
#import "Reachability.h"
#import "ActivitySinglton.h"
@interface ActivityViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) NSString *filePath;
@property (nonatomic,assign) BOOL upload;
@end

@implementation ActivityViewController

- (void)dealloc

{
    [_filePath release];
    [_pongRefreshControl release];
    [_hub release];
    [_activityArray release];
    [_mytableView release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"活动";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:UIBarMetricsDefault];
    
    //tableCell
    self.mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, HEIGHT - 49 - 64)];
    self.mytableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mytableView];
    self.mytableView.delegate = self;
    self.mytableView.dataSource = self;
    
    //注册cell
    [self.mytableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];
    //全局的要初始化
    self.activityArray = [[NSMutableArray alloc] init];
    
    //打开数据库
    [[ActivitySinglton shareDataBaseSingleton] openDB];
    [[ActivitySinglton shareDataBaseSingleton] createActivityTable];
    NSMutableArray *array = [[ActivitySinglton shareDataBaseSingleton] selectAllActivity];

#warning 好习惯!!!
    //好习惯(拷贝一份)
    self.activityArray = [NSMutableArray arrayWithArray:array];
    
    [self.mytableView reloadData];
    //上拉下拉
    [self addHeaderRefresh];
    [self addFooterRefresh];
    //自动刷新
    [self.mytableView headerBeginRefreshing];

    self.upload = NO;
}


//MJ下拉
-(void)addHeaderRefresh
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            NSLog(@"没有网络连接");
            // 没有网络连接
            break;
        case ReachableViaWWAN:
            // 使用3G网络
            NSLog(@"使用3G网络");
            
            break;
        case ReachableViaWiFi:
            // 使用WiFi网络
            NSLog(@"使用WiFi网络");
            
            //MJ下拉方法
            [self.mytableView addHeaderWithCallback:^{
                self.upload = NO;
                //获取数据
                [self getAFNData];
            }];
            
            break;
    }
}


-(void)getAFNData{
    //加载菊花
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"请耐心等待....";
    [AppTools getDataWithkey:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" block:^(id result) {
        
        if (self.upload == NO) {
            //清空数据
            [self.activityArray removeAllObjects];
            [[ActivitySinglton shareDataBaseSingleton] dropActivityTable];
            [[ActivitySinglton shareDataBaseSingleton] createActivityTable];
        }
        
        
        NSMutableDictionary *Bigdic = result;
        NSMutableArray *array = [Bigdic objectForKey:@"events"];
        for (NSMutableDictionary  *dic in array) {
            Activity *activity = [[Activity alloc] init];
            [activity setValuesForKeysWithDictionary:dic];
            [self.activityArray addObject: activity];
            [activity release];
            
            //插入到数据库
            [[ActivitySinglton shareDataBaseSingleton] insertActivityTable:activity];
            
        }
        [self.mytableView headerEndRefreshing];
        //刷新数据    //是重走两个必须实现的那两个协议方法
        [self.mytableView reloadData];
        [self.hub hide:YES];
    }];
}
//上拉
-(void)addFooterRefresh
{
    //当上拉时候触发该方法
    [self.mytableView addFooterWithCallback:^{
        
        NSLog(@"上拉");
        self.upload = YES;
        [self.mytableView footerEndRefreshing];
        
    }];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.activityArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellI = @"cell";
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellI];
    //模型传数据
    Activity *activity = [self.activityArray objectAtIndex:indexPath.row];
    cell.activity = activity;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    Activity *activity = [self.activityArray objectAtIndex:indexPath.row];
    detailVC.activity = activity;
    [self.navigationController pushViewController: detailVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
